package com.cai310.lottery.entity.lottery.jczq;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.type.EnumType;

import com.cai310.entity.CreateMarkable;
import com.cai310.entity.IdEntity;
import com.cai310.entity.UpdateMarkable;
import com.cai310.lottery.JczqConstant;
import com.cai310.lottery.cache.CacheConstant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.support.Item;
import com.cai310.lottery.support.jczq.ItemBF;
import com.cai310.lottery.support.jczq.ItemBQQ;
import com.cai310.lottery.support.jczq.ItemJQS;
import com.cai310.lottery.support.jczq.ItemSPF;
import com.cai310.lottery.support.jczq.JczqContentBean;
import com.cai310.lottery.support.jczq.JczqUtil;
import com.cai310.lottery.support.jczq.PassMode;
import com.cai310.lottery.support.jczq.PassType;
import com.cai310.lottery.support.jczq.PlayType;
import com.cai310.lottery.support.jczq.SchemeType;
import com.cai310.utils.DateUtil;

@Table(name = JczqSingleScheme.TABLE_NAME)
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = CacheConstant.H_JCZQ_MATCH_CACHE_REGION)
public class JczqSingleScheme extends IdEntity implements CreateMarkable, UpdateMarkable {
	private static final long serialVersionUID = -7664713483528947069L;
 
	public static final String TABLE_NAME = com.cai310.lottery.Constant.LOTTERY_TABLE_PREFIX + "JCZQ_SINGLE_SCHEME";
	/** 用户编号 */
	private Long userId;
	/** 创建时间 */
	private Date createTime;

	/** 最后更新时间 */
	private Date lastModifyTime;

	/** 版本号,用于实现乐观锁 */
	private Integer version;

	/** 投注内容2011301-001*3,1+2011302-001*1$3|2011301-001*3,1+2011302-001*1$3|2011301-001*3,1+2011302-001*1$3  */
	private String contents;

	/** 倍数 1|2|3|14 1倍，2倍...*/
	private String multiples;
	
	/** 金额 2|8|14 ，2元...*/
	private String costs;
	
	/** 金额 1|4|7 ，1注...*/
	private String units;
	
	/**编号串...*/
	private String schemes;
	/** 过关 P2_1|P2_1|P2_1 ..*/
	private String passTypes;
	
	/** 方案类型 */
	private SchemeType schemeType;
	/**
	 * 彩种类型
	 * 
	 * @see com.cai310.lottery.common.Lottery
	 */
	private Lottery lotteryType;
	
	
	private PlayType playType;
	
	public JczqSingleScheme(){}
	public JczqSingleScheme(List<JczqContentBean> list) {
		StringBuffer multiples = new StringBuffer();
		StringBuffer units = new StringBuffer();
		StringBuffer costs = new StringBuffer();
		StringBuffer contents = new StringBuffer();
		StringBuffer passTypes = new StringBuffer();
		StringBuffer schemes = new StringBuffer();
		for (JczqContentBean jczqContentBean : list) {
			multiples.append(jczqContentBean.getMultiple()+"|");
			units.append(jczqContentBean.getUnits()+"|");
			costs.append(jczqContentBean.getCost()+"|");
			contents.append(jczqContentBean.getContent()+"|");
			passTypes.append(jczqContentBean.getPassTypes().get(0)+"|");
			schemes.append("未发起|");
		}
		this.playType = PlayType.SPF;
		this.lotteryType = Lottery.JCZQ;
		this.schemeType = SchemeType.SIMPLE_PASS;
		this.multiples = multiples.delete(multiples.length()-"|".length(), multiples.length()).toString();
		this.units = units.delete(units.length()-"|".length(), units.length()).toString();
		this.costs = costs.delete(costs.length()-"|".length(), costs.length()).toString();
		this.contents = contents.delete(contents.length()-"|".length(), contents.length()).toString();
		this.passTypes = passTypes.delete(passTypes.length()-"|".length(), passTypes.length()).toString();
		this.schemes = schemes.delete(schemes.length()-"|".length(), schemes.length()).toString();
		
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(insertable = false)
	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, updatable = false)
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @return the {@link #version}
	 */
	@Version
	@Column(nullable = false)
	public Integer getVersion() {
		return version;
	}

	/**
	 * @param version the {@link #version} to set
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}
	@Column
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}
	@Transient
	public String getContentsStr() {
		return contents.replaceAll("\\|", "\r\n");
	}
	@Transient
	public Integer getLength() {
		return costs.split("\\|").length;
	}
	@Transient
	public String getStrByIndex(Integer index,Integer type) {
		if(0==type){
			return contents.split("\\|")[index];
		}else if(1==type){
			return multiples.split("\\|")[index];
		}else if(2==type){
			return units.split("\\|")[index];
		}else if(3==type){
			return costs.split("\\|")[index];
		}else if(4==type){
			return passTypes.split("\\|")[index];
		}else if(5==type){
			return schemes.split("\\|")[index];
		}else{
			return null;
		}
		 
	}
	@Transient
	public String getSchemesStr() {
		if(StringUtils.isBlank(schemes))return null;
		String[] schemesArr = schemes.split("\\|");
		StringBuffer schemes = new StringBuffer();
		for (int i = 0; i < schemesArr.length; i++) {
			if(StringUtils.isNotBlank(schemesArr[i])&&!"未发起".equals(schemesArr[i].trim())){
				schemes.append("<a href=\"/jczq/scheme!show.action?schemeNumber="+schemesArr[i]+"\" target=\"_blank\">"+schemesArr[i]+"</a>").append("<br/>");
			}else{
				schemes.append(schemesArr[i]).append("|");
			}
		}
		return schemes.delete(schemes.length()-"|".length(), schemes.length()).toString();
	}
    @Transient
	public void updateSchemes(Integer index,String schemeNum) throws DataException {
		if(StringUtils.isBlank(schemes)){
			StringBuffer schemes = new StringBuffer();
			for (int i = 0; i < getLength(); i++) {
				if(index==i){
					schemes.append(schemeNum).append("|");
				}else{
					schemes.append("未发起").append("|");
				}
			}
			this.schemes = schemes.delete(schemes.length()-"|".length(), schemes.length()).toString();
		}else{
			String[] schemesArr = schemes.split("\\|");
			StringBuffer schemes = new StringBuffer();
			for (int i = 0; i < getLength(); i++) {
				if(index==i){
					if(StringUtils.isNotBlank(schemesArr[i])&&!"未发起".equals(schemesArr[i].trim())){
						throw new DataException("不能重复发起方案");
					}
					schemes.append(schemeNum).append("|");
				}else{
					schemes.append(schemesArr[i]).append("|");
				}
			}
			this.schemes = schemes.delete(schemes.length()-"|".length(), schemes.length()).toString();
		}
		
	}
	@Lob
	@Column
	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}
	@Lob
	@Column
	public String getMultiples() {
		return multiples;
	}

	public void setMultiples(String multiples) {
		this.multiples = multiples;
	}
	@Lob
	@Column
	public String getCosts() {
		return costs;
	}

	public void setCosts(String costs) {
		this.costs = costs;
	}
	@Lob
	@Column
	public String getUnits() {
		return units;
	}

	public void setUnits(String units) {
		this.units = units;
	}
	@Lob
	@Column
	public String getSchemes() {
		return schemes;
	}

	public void setSchemes(String schemes) {
		this.schemes = schemes;
	}
	@Lob
	@Column
	public String getPassTypes() {
		return passTypes;
	}

	public void setPassTypes(String passTypes) {
		this.passTypes = passTypes;
	}
	@Type(type = "org.hibernate.type.EnumType", parameters = {
			@Parameter(name = EnumType.ENUM, value = "com.cai310.lottery.common.Lottery"),
			@Parameter(name = EnumType.TYPE, value = Lottery.SQL_TYPE) })
	@Column(nullable = false, updatable = false)
	public Lottery getLotteryType() {
		return lotteryType;
	}

	public void setLotteryType(Lottery lotteryType) {
		this.lotteryType = lotteryType;
	}
	@Type(type = "org.hibernate.type.EnumType", parameters = {
			@Parameter(name = EnumType.ENUM, value = "com.cai310.lottery.support.jczq.PlayType"),
			@Parameter(name = EnumType.TYPE, value = Lottery.SQL_TYPE) })
	@Column(nullable = false, updatable = false)
	public PlayType getPlayType() {
		return playType;
	}

	public void setPlayType(PlayType playType) {
		this.playType = playType;
	}
	@Type(type = "org.hibernate.type.EnumType", parameters = {
			@Parameter(name = EnumType.ENUM, value = "com.cai310.lottery.support.jczq.SchemeType"),
			@Parameter(name = EnumType.TYPE, value = Lottery.SQL_TYPE) })
	@Column(nullable = false, updatable = false)
	public SchemeType getSchemeType() {
		return schemeType;
	}

	public void setSchemeType(SchemeType schemeType) {
		this.schemeType = schemeType;
	}

	
}
